#include "scanner.h"

// to build, just run "make"
// then ./parser input.txt output.txt

int main(int argc, const char **argv){
  // args in order:
  // 1: PL0 code input filename
  // 2: output filename
  // 3: VM input file (assembly), will run in any hw1 Virtual Machine.
  //    Isn't actually used as it is not required to output to file.

  char inputFileName[MAX_PROGRAM_LENGTH], outputFileName[MAX_PROGRAM_LENGTH], vmInputFilename[MAX_PROGRAM_LENGTH];
  strcpy(inputFileName, argv[1]);
  FILE* inPtr = fopen(inputFileName, "r");
  strcpy(outputFileName, argv[2]);
  FILE* outPtr = fopen(outputFileName, "w");
  strcpy(vmInputFilename, argv[3]);
  FILE* vmInputPtr = fopen(vmInputFilename, "w");

  if(inPtr == NULL){
    printf("File not found: %s", inputFileName);
    return 0;
  }
  if(outPtr == NULL){
    printf("File not found: %s", outputFileName);
    return 0;
  }

  // scan in PL0 code, get lexeme list
  char lexList[MAX_PROGRAM_LENGTH];
  scan_file(inPtr, outPtr, lexList);
  //printf("SUP WORLD %s\n", lexList);

  // parse lexeme list (validate code, get symbol table)
  // generate assembly for vm
  instruction assemblyCode[MAX_CODE_LENGTH];
  int codeLength = parseLexemeList(outPtr, lexList, assemblyCode);

  fprintf(outPtr, "No errors, program is syntactically correct\n\n");

  // run assembly on vm, print output
  runVM(outPtr, vmInputPtr, assemblyCode, codeLength);

  fclose(inPtr);
  fclose(outPtr);
  fclose(vmInputPtr);

  return 0;
}
